package cn.cheegon.blog.controller;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.common.api.CommonResult;
import cn.cheegon.blog.entity.dto.CmsBlogDto;
import cn.cheegon.blog.entity.req.*;
import cn.cheegon.blog.entity.rsp.LoginRsp;
import cn.cheegon.blog.entity.rsp.UserInfoRsp;
import cn.cheegon.blog.model.CmsBlog;
import cn.cheegon.blog.model.UmsAdmin;
import cn.cheegon.blog.service.UmsAdminService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author cheegon
 * @since 2024-06-21
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/user/*")
public class UmsAdminController extends AdminController {
    @Autowired
    private UmsAdminService adminService;

    @GetMapping(value = "list")
    public CommonResult<CommonPage<UserInfoRsp>> list(
            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        return CommonResult.success(adminService.list(pageNum, pageSize));
    }

    @PostMapping("edit")
    public CommonResult edit(@RequestBody UserReq req) {
        checkAdmin();
        adminService.update(BeanUtil.toBean(req, UmsAdmin.class));
        return CommonResult.success();
    }

    @PostMapping("changeType")
    public CommonResult changeType(@RequestBody UserTypeReq req) {
        checkAdmin();
        adminService.changeType(req.getId(), req.getType());
        return CommonResult.success();
    }

    @PostMapping("changeStatus")
    public CommonResult changeStatus(@RequestBody UserStatusReq req) {
        checkAdmin();
        adminService.changeStatus(req.getId(), req.getStatus());
        return CommonResult.success();
    }

    @GetMapping(value = "del/{id}")
    public CommonResult del(@PathVariable Long id) {
        adminService.delete(id);
        return CommonResult.success();
    }

}
